<template>
	<view>
		<u-notify ref="uNotify"></u-notify>
		<u-row customStyle="margin-bottom: 10px">
			<u-col span="2.77">
				<u-button  iconStyle="font-size: 20px" type="primary" size="small" :bold="true"	align="center" customStyle="fontSize:15px"
				v-if="showbutton && outid!=null" icon="arrow-down" @click="nextWork">处理</u-button>
			</u-col>
			<u-col span="2.77" offset="0.3">
				<u-button  v-if="showget" @click="modalShow = true" type="primary" :plain="true" 
				 size="small" customStyle="fontSize:15px">领取</u-button>
			</u-col>
			<u-col span="2.77" offset="0.3">
				<u-button  v-if="showbutton && outid!=null" @click="show = true" type="primary" :plain="true" 
				 size="small" customStyle="fontSize:15px">更多</u-button>
			</u-col>
			<u-col span="2.77" offset="0.3">
				<u-button icon="arrow-down" iconStyle="font-size: 14px" :bold="true" align="center" 
					@click='gorecord' type="primary" size="small" customStyle="fontSize:11px">流程记录</u-button>
			</u-col>
		</u-row>
		<!-- 		<u-button @click="show = true">处理</u-button>
		<u-button @click="showrecode = true">评审记录</u-button> -->
		<!-- <u-picker :show="show" @confirm="confirm" @change="changeHandler" :columns="columns"></u-picker> -->
		<u-action-sheet :actions="list"  cancelText="取消" @select="confirm" @close="show=false" :show="show"></u-action-sheet>
		<u-popup :show="shownext" @close="shownext=false" @open="open" mode="bottom" >	
			<u--form labelPosition="left" labelWidth="100">
				<u-form-item label="选择下一步骤" prop="selectedActId" borderBottom ref="item1">
					<u-radio-group v-model="selectedActId" placement="column" @change="actionChange">
						<u-radio :customStyle="{marginBottom: '8px'}" v-for="(item, index) in actionOptions"
							:key="index" :label="item.label" :name="item.value" labelSize="15px">
						</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="选择处理人" prop="selectedUserId" borderBottom ref="item1">
					<u-list height="200px">
						<u-radio-group v-model="selectedUserId" placement="column" >
							<u-radio :customStyle="{marginBottom: '8px'}" v-for="(item, index) in userOptions" :key="index"
								:label="item.label" :name="item.value">
							</u-radio>
						</u-radio-group>
					</u-list>	
				</u-form-item>
				<u-form-item label="审批意见" prop="remark" borderBottom ref="item1">
					<view style="display: flex;  flex-direction: column; width: 100%;">
						<view style="display: flex;">
							<u-button size="mini" customStyle="fontSize:15px" color="#d19a57" @click="remark = '拟同意'">拟同意</u-button>
							<u-button size="mini" customStyle="fontSize:15px" color="#d19a57" @click="remark = '同意'">同意</u-button>
							<u-button size="mini" customStyle="fontSize:15px" color="#d19a57" @click="remark = '驳回'">驳回</u-button>
						</view>
						<u-textarea v-model="remark" borderBottom placeholder="请填写审批意见" border="none"></u-textarea>
					</view>
					
					
					<!-- <u-icon slot="right" name="arrow-right"></u-icon> -->
				</u-form-item>
			</u--form>
			<u-button type="primary"  @click="save" customStyle="fontSize:16px">确定</u-button>
		</u-popup>
		<u-modal :show="modalShow" title="提示" content="你确定要领取吗？" :showCancelButton="true" @confirm="GetIt" @cancel="modalShow = false"></u-modal>
	</view>
</template>

<script>
	import {
		getprojlist,
		GetNextOption,
		GoNext,
		GetCurrentAct,
	} from '@/common/api.js'
	export default {
		props: {
			outid: {},
			flowid: {},
			deptid: {},
			dataname: {},
			formstate: {},
			urlparam: {},
			outshowbutton: {}, //外部控制是否显示处理按键
			outmodalShow: {}, 
		},
		data() {
			return {
				showget:false,
				showbutton:true,
				user:{},
				currentact:{},
				currentrecord:{},
				selectedUserId: "",
				userOptions: [],
				remark: "",
				show: false,
				showrecode: false,
				shownext: false,
				selectedActId: null,
				isDataLoaded: false,
				modalShow: false,
				actionOptions: [],
				notifyData: {
					message: '发送成功',
					type: 'success',
					color: '#ffffff',
					bgColor: '',
					fontSize: 15,
					duration: 3000,
					safeAreaInsetTop: false
				},
				cometype: "通过",
				columns: ['驳回', '移交'],
				list: [
					{
						name: '驳回',idx:0,
					},
					{
						name: '移交',idx:1,
					}
				],
			}
		},
		watch: {

			outid: {

				handler(newv, oldv) {
					this.initOptions()
					this.initAll()
					if(this.outmodalShow == 'true'){
						this.nextWork()
					}
				}

			}
		},
		onLoad(option) {
			this.user = JSON.parse(uni.getStorageSync("$userinfo"))
			// this.buildid = option.buildid
			// if (option.buildid)
			this.initAll()		
		},
		onShow()
		{
			this.user = JSON.parse(uni.getStorageSync("$userinfo"))
			this.initAll()
		},
		mounted() {
			// this.getdate()

		},

		methods: {
			open() {
				this.initOptions()
			},
			confirm(e) {
			
				// this.cometype = e.name
			
				this.cometype = this.columns[e.idx]
				this.show = false
				this.shownext = true
			},
			nextWork(){
				this.cometype = '通过'
				this.show = false
				this.shownext = true
			},
			gorecord() {
				uni.navigateTo({
					url: '../flowrecord?id=' + this.outid
				})
			},
			showmap() {
				uni.navigateTo({
					url: 'map?buildid=' + this.buildid
				})
			},
			initOptions() {
				this.selectedActId = ""
				// console.log(this.outid + "," + this.cometype + "," + this.flowid)
				GetNextOption(this.outid, this.cometype, this.flowid, this.deptid).then(res => {			
					this.nextoptions = res
					this.isDataLoaded = true;
					this.actionOptions = []
					this.actionIdx = {}
					this.userOptions = []
					// for (var i = 0; i < res.data.length; i++) 
					res.forEach(element => {

						// var element = res.data[i]

						// let data = this.formstate
						// console.log(eval(element.Condition)+data)//流程主体用data
						// // eval("console.log(data)")
						// if (element.Condition && !eval(element.Condition) && this.cometype=="通过" && res.data.length>1)//流程前进和超过2个分支时用
						//     continue
						this.actionOptions.push({
							label: element.Action.ActionName,
							value: element.Action.Id
						});
						console.log(this.actionOptions)
						this.actionIdx[element.Action.Id] = element.Users
					})
					// res.data.forEach(element => {
					//     console.log('xxxxxxxxx')
					//     // console.log(this)
					//     eval("console.log(this+'bbbbbb')")
					//     if (element.Condition && !eval("("+element.Condition+")"))
					//         return
					//     this.actionOptions.push({
					//         label: element.Action.ActionName,
					//         value: element.Action.Id
					//     }.bind(this));
					//     this.actionIdx[element.Action.Id] = element.Users
					// });

				})
			},
			actionChange() {
				this.selectedUserId=""
				this.userOptions = []
				this.actionIdx[this.selectedActId].forEach(x => {
					this.userOptions.push({
						label: x.Name,
						value: x.Id
					})
				})
			},
			GetIt()
			{
				this.confirmLoading = true
				
				GoNext(this.actionOptions[0].value, "领取", this.user.Id, this.remark, this.flowid, this.outid, this
					.dataname, this.urlparam).then(res => {
					if (res.status == 200) {
						var route = "/dashboard/todo"
						this.setfresshroute(route);
						this.$message.success('领取成功。');
						this.Visible = false
						this.isDataLoaded = false
					}
					this.confirmLoading = false
					uni.navigateBack()
					// this.initAll()
					// if(this.cometype=="通过" && res.data.FlowAction.WriteBack)
					// {
					// let data = this.formstate;
					// console.log(this.formstate+"主体")
					// eval(res.data.FlowAction.WriteBack)
					// this.$emit("writeback",data)//回写主体
					// }
				})
			},
			save() {
				this.confirmLoading = true
				GoNext(this.selectedActId, this.cometype, this.selectedUserId, this.remark, this.flowid, this.outid, this
					.dataname, this.urlparam).then(res => {
					if (res.status == 200) {
						var route = "/dashboard/todo"
						this.setfresshroute(route);		
						// this.$message.success('发送成功。');
						this.Visible = false
						this.isDataLoaded = false
						this.confirmLoading = false	
					}
					this.$refs.uNotify.show(this.notifyData)
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
					// this.initAll()
					// if(this.cometype=="通过" && res.data.FlowAction.WriteBack)
					// {
					// let data = this.formstate;
					// console.log(this.formstate+"主体")
					// eval(res.data.FlowAction.WriteBack)
					// this.$emit("writeback",data)//回写主体
					// }
				})
			},
			            initAll() {
							this.user = JSON.parse(uni.getStorageSync("$userinfo"))
			                if(this.outid)
			                {
			                GetCurrentAct(this.outid).then(res => {
			                        this.currentact = res.FlowAction
			                        this.currentrecord = res
									console.log(this.outshowbutton,'zzzz');
									// console.log(this.user)
									// console.log(this.currentrecord)
			                        // this.currentflow = res.data.FlowAction.SysFlow
			                        //  alert(this.user.Id+this.user.Name+this.currentrecord.UserId)
			                        if ((this.currentrecord.UseId != null && this.currentrecord.UseId != this.user.Id && this.user.Name != 'admin') || this.outshowbutton==false) {
										
			                            this.showbutton = false
			                        } else {
			                            this.showbutton = true
			                        }
									if(this.currentrecord.FlowAction.CanGet && this.currentrecord.UseId != this.user.Id)
									{
										
										this.showget=true
									}
									if(this.currentrecord.FlowAction.CanDoRoleList && this.currentrecord.FlowAction.CanDoRoleList!="[]")
									{
										console.log(this.currentrecord)
										var list = JSON.parse(this.currentrecord.FlowAction.CanDoRoleList)
										
										this.showbutton=list.some(e=>this.user.UserRoleList.some(e2=>e==e2.RoleId))
									}
									
			                })
			            }
	
			            },

		}
	}
</script>

<style>

</style>